Converting URL"s to hyperlinks.
Converting URL"s to hyperlinks.
am 27.08.2009 19:56:14 von John Meyer
What sort of function would I need if I wanted to convert those URLs
from plain jane text?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Converting URL"s to hyperlinks.
am 29.08.2009 01:22:20 von LinuxManMikeC
If you have short php tags enabled, assuming you put the url in a
variable called $url...
otherwise...
or a myriad of other ways...
Depending if there are any special characters involved, there are a
couple functions that would be useful for preparing the url before
outputing it in html, but I don't remember them off the top of my
head. RTFM for that.
On Thu, Aug 27, 2009 at 11:56 AM, John
Meyer wrote:
> What sort of function would I need if I wanted to convert those URLs from
> plain jane text?
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Converting URL"s to hyperlinks.
am 29.08.2009 09:07:18 von Eric Lee
DQotLS0tLSBPcmlnaW5hbCBNZXNzYWdlIC0tLS0tIA0KRnJvbTogIkpvaG4g TWV5ZXIiIDxqb2hu
bWV5ZXJAcHVlYmxvY29tcHV0aW5nLmNvbT4NClRvOiA8cGhwLWdlbmVyYWxA bGlzdHMucGhwLm5l
dD4NClNlbnQ6IEZyaWRheSwgQXVndXN0IDI4LCAyMDA5IDE6NTYgQU0NClN1 YmplY3Q6IFtQSFBd
IENvbnZlcnRpbmcgVVJMJ3MgdG8gaHlwZXJsaW5rcy4NCg0KDQo+IFdoYXQg c29ydCBvZiBmdW5j
dGlvbiB3b3VsZCBJIG5lZWQgaWYgSSB3YW50ZWQgdG8gY29udmVydCB0aG9z ZSBVUkxzIA0KPiBm
cm9tIHBsYWluIGphbmUgdGV4dD8NCj4gDQoNCllvdSBzaG91bGQgZW5jb2Rl IHRoZSB1cmwgYmVm
b3JlIHByaW50aW5nIGFzIHVzdWFsIHdheQ0KDQokdXJsID0gaHRtbGVudGl0 aWVzKCdodHRwOi8v
d3d3Lm15c2l0ZS5jb20vaW5kZXgucGhwP2FjdD0xJnQ9MTAnKTsNCg0KZWNo byAiPGEgaHJlZj1c
InskdXJsfVwiPm15c2l0ZTwvYT4iOw0KDQoNCi0gRXJpYw0KDQo+IC0tIA0K PiBQSFAgR2VuZXJh
bCBNYWlsaW5nIExpc3QgKGh0dHA6Ly93d3cucGhwLm5ldC8pDQo+IFRvIHVu c3Vic2NyaWJlLCB2
aXNpdDogaHR0cDovL3d3dy5waHAubmV0L3Vuc3ViLnBocA0KPiANCj4=
Re: Converting URL"s to hyperlinks.
am 29.08.2009 22:45:42 von news.NOSPAM.0ixbtqKe
On Fri, 28 Aug 2009 17:22:20 -0600, LinuxManMikeC wrote:
>
*Groan*
Throw any random web site to an HTML validator
and you're likely to see this kind of slop all
over.
The correct solution is of course:
$u = htmlspecialchars ($url);
echo "";
[A more elaborate way to flay this feline is
included below.]
/Nisse
/* Reworked from slightly different code.
Bugs may have been introduced. */
function url_to_links ($url)
{
if (preg_match ('@^([a-z]+://)(.*)@i', $url, $m)) {
$prfx = $m[1];
$path = $m[2];
} else {
return htmlspecialchars ($url);
}
$url_sofar = $prfx;
$links = htmlspecialchars ($prfx);
$segs = explode ('?', $path, 2);
if (isset ($segs[1]))
$query = $segs[1];
$segs = explode ('/', $segs[0]);
for ($segn = 0; $segn < count ($segs); $segn++) {
$url_sofar .= $segs[$segn];
if (isset ($segs[$segn+1]))
$url_sofar .= '/';
if ($segs[$segn] !== '') {
$links .= ''
. htmlspecialchars ($segs[$segn]) . '';
}
if (isset ($segs[$segn+1]))
$links .= '/';
}
if (isset ($query)) {
$url_sofar .= "?$query";
$links .= '?' . htmlspecialchars ($query) . '';
}
return $links;
}
$u = 'https://ebagwa.example/abd/def/ghi?s=t&u=v&w=x&y=z';
$u_h = htmlspecialchars ($u);
$links = url_to_links ($u);
header ('Content-Type: text/html');
echo <<<_
"http://www.w3.org/TR/html4/strict.dtd">
url_to_links()
$u_h
↓
$links
_;
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Converting URL"s to hyperlinks.
am 30.08.2009 00:19:05 von LinuxManMikeC
2009/8/29 Nisse Engström :
> On Fri, 28 Aug 2009 17:22:20 -0600, LinuxManMikeC wrote:
>
>>
>
> *Groan*
>
> Throw any random web site to an HTML validator
> and you're likely to see this kind of slop all
> over.
>
> The correct solution is of course:
>
> Â $u =3D htmlspecialchars ($url);
> Â echo "";
>
>
Right... you do realize that you validate the HTML output of the
executed PHP script, not the PHP script itself. All you really did
was just show another way to skin the same cat. Get over yourself.
As for your "more elaborate example", I'm sure that heredoc will
validate nicely. It also wouldn't hurt to read a book on algorithms
and rethink your code so you aren't processing the same data over and
over again. I "see this kind of slop all over."
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Converting URL"s to hyperlinks.
am 30.08.2009 00:38:50 von news.NOSPAM.0ixbtqKe
On Sat, 29 Aug 2009 16:19:05 -0600, LinuxManMikeC wrote:
> As for your "more elaborate example", I'm sure that heredoc will
> validate nicely.
It does.
> and rethink your code so you aren't processing the same data over and
> over again. I "see this kind of slop all over."
Touché!
Would you believe that's on my todo list?
Thanks for the reminder. :-)
/Nisse
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Converting URL"s to hyperlinks.
am 30.08.2009 00:47:47 von LinuxManMikeC
2009/8/29 Nisse Engström :
> On Sat, 29 Aug 2009 16:19:05 -0600, LinuxManMikeC wrote:
>
>> As for your "more elaborate example", I'm sure that heredoc will
>> validate nicely.
>
> It does.
>
Perhaps you haven't met a few good friends of mine. Their names are
html, head, and body. So what crawled up your backside while you were
reading my example?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Converting URL"s to hyperlinks.
am 30.08.2009 01:02:03 von news.NOSPAM.0ixbtqKe
On Sat, 29 Aug 2009 16:47:47 -0600, LinuxManMikeC wrote:
> 2009/8/29 Nisse Engström :
>> On Sat, 29 Aug 2009 16:19:05 -0600, LinuxManMikeC wrote:
>>
>>> As for your "more elaborate example", I'm sure that heredoc will
>>> validate nicely.
>>
>> It does.
>>
>
> Perhaps you haven't met a few good friends of mine. Their names are
> html, head, and body.
The html, head and body elements are all there. They are
mandatory. The tags however, are optional.
/Nisse
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
OpenCart
am 30.08.2009 01:02:41 von HallMarc Websites
I am wondering if anyone here can provide some hands-on feedback about =
this OS cart such as can it handle large catalogues of products and high =
amount of traffic? If you don't know about this cart or know of a better =
cart that is more closely suited to fulfill an enterprise level =
ecommerce need.
Thank you,
Marc Hall
HallMarc Websites
610.446.3346
=20
__________ Information from ESET Smart Security, version of virus =
signature database 4380 (20090829) __________
The message was checked by ESET Smart Security.
http://www.eset.com
=20
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Converting URL"s to hyperlinks.
am 03.09.2009 02:41:46 von Daevid Vincent
Maybe I misunderstood the OP, but wouldn't this (or something like it) =
be
easier and cleaner than that mess below??
$url =3D preg_replace("/(\w+@\w+\.[a-zA-Z]{2,3})/i", "
href=3D'mailto:$1'>$1", $url);
$url =3D =
preg_replace("/\s(http:\/\/)?(\w*\.?\w*\.[a-zA-Z]{2,3}.*?\s) /i", "
href=3D'http://$2' target=3D'_blank'>$2", $url);
> -----Original Message-----
> From: Nisse Engström [mailto:news.NOSPAM.0ixbtqKe@luden.se]=20
> Sent: Saturday, August 29, 2009 1:46 PM
> To: php-general@lists.php.net
> Subject: Re: [PHP] Converting URL's to hyperlinks.
>=20
> On Fri, 28 Aug 2009 17:22:20 -0600, LinuxManMikeC wrote:
>=20
> >
>=20
> *Groan*
>=20
> Throw any random web site to an HTML validator
> and you're likely to see this kind of slop all
> over.
>=20
> The correct solution is of course:
>=20
> $u =3D htmlspecialchars ($url);
> echo "";
>=20
>=20
> [A more elaborate way to flay this feline is
> included below.]
>=20
>=20
> /Nisse
>=20
>=20
> /* Reworked from slightly different code.
> Bugs may have been introduced. */
>=20
>
>=20
> function url_to_links ($url)
> {
> if (preg_match ('@^([a-z]+://)(.*)@i', $url, $m)) {
> $prfx =3D $m[1];
> $path =3D $m[2];
> } else {
> return htmlspecialchars ($url);
> }
>=20
> $url_sofar =3D $prfx;
> $links =3D htmlspecialchars ($prfx);
>=20
> $segs =3D explode ('?', $path, 2);
> if (isset ($segs[1]))
> $query =3D $segs[1];
> $segs =3D explode ('/', $segs[0]);
>=20
> for ($segn =3D 0; $segn < count ($segs); $segn++) {
> $url_sofar .=3D $segs[$segn];
> if (isset ($segs[$segn+1]))
> $url_sofar .=3D '/';
>=20
> if ($segs[$segn] !== '') {
> $links .=3D '
'">'
> . htmlspecialchars ($segs[$segn]) . '';
> }
>=20
> if (isset ($segs[$segn+1]))
> $links .=3D '/';
> }
>=20
> if (isset ($query)) {
> $url_sofar .=3D "?$query";
> $links .=3D '?
> . '">' . htmlspecialchars ($query) . '';
> }
>=20
> return $links;
> }
>=20
> $u =3D 'https://ebagwa.example/abd/def/ghi?s=3Dt&u=3Dv&w=3Dx&y=3Dz' ;
> $u_h =3D htmlspecialchars ($u);
> $links =3D url_to_links ($u);
>=20
> header ('Content-Type: text/html');
>=20
> echo <<<_
>
> "http://www.w3.org/TR/html4/strict.dtd">
>
url_to_links()
>=20
>
> $u_h
> ↓
> $links
>
>=20
> _;
>=20
> --=20
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>=20
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Converting URL"s to hyperlinks.
am 04.09.2009 16:46:13 von Lupus Michaelis
Daevid Vincent a écrit :
> Maybe I misunderstood the OP,
OP ?
> but wouldn't this (or something like it) be
> easier and cleaner than that mess below??
No, it's dirty too.
> $url = preg_replace("/(\w+@\w+\.[a-zA-Z]{2,3})/i", "
> href='mailto:$1'>$1", $url);
This violate the numerous RFC about mail addresses, and some other
stuffs.
>
> $url = preg_replace("/\s(http:\/\/)?(\w*\.?\w*\.[a-zA-Z]{2,3}.*?\s) /i", "
> href='http://$2' target='_blank'>$2", $url);
Same as previously. What about .info, .museum and so on tld ? The
filter_var is well suited for this kind of job. Oh, and your regex isn't
smart (you use the case insensitivity flag, but seek A-Z characters...) :D
--
Mickaël Wolff aka Lupus Michaelis
http://lupusmic.org
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Converting URL"s to hyperlinks.
am 04.09.2009 22:49:54 von Daevid Vincent
=20
> -----Original Message-----
> From: Lupus Michaelis [mailto:mickael+php@lupusmic.org]=20
> Sent: Friday, September 04, 2009 7:46 AM
> To: php-general@lists.php.net
> Subject: Re: [PHP] Converting URL's to hyperlinks.
>=20
> Daevid Vincent a =E9crit :
> > Maybe I misunderstood the OP,
> OP ?
Original Poster
> > but wouldn't this (or something like it) be
Note the "something like it". I didn't write his app, just provided a
starting point.
> > $url =3D preg_replace("/(\w+@\w+\.[a-zA-Z]{2,3})/i", "
> > href=3D'mailto:$1'>$1", $url);
> This violate the numerous RFC about mail addresses, and some other=20
> stuffs.
Blah blah blah.
I've used this code for about 6 years now and have yet to find emails =
that
it didn't work for. If someone has some funky (whacky) RFC extremity, =
then
so be it. That's their problem. Most people have NORMAL emails that =
follow
the above.
But you are correct, I have revised it to be a little more forgiving of =
some
allowed characters...
preg_replace("/([\w\.\-_]+@[\w\.\-_]+\.\w{2,6})/i",
> >=20
> > $url =
> preg_replace("/\s(http:\/\/)?(\w*\.?\w*\.[a-zA-Z]{2,3}.*?\s) /i", "
> > href=3D'http://$2' target=3D'_blank'>$2", $url);
>=20
> Same as previously. What about .info, .museum and so on tld ?=20
What about them? It's going to depend on how/where you use this. In my =
case,
2 and 3 letter domains are all I encounter. It's trivial to make it =
{2,6} if
you really are going to encounter a .museum domain. DOUBTFUL, but sure, =
I'll
concede the three extra letters. ;-)
However, you point out a few edge cases and so I optimized mine and now =
use
this one:
http://snipplr.com/view/2371/regex-regular-expression-to-mat ch-a-url/
> > Oh, and your regex isn't=20
> smart (you use the case insensitivity flag, but seek A-Z=20
> characters...) :D
Noted. Thanks for the optimization.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Converting URL"s to hyperlinks.
am 04.09.2009 22:54:14 von Tom Chubb
--000e0cd1fadaeda2f50472c6b3cf
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
2009/9/4 Daevid Vincent
>
>
> > -----Original Message-----
> > From: Lupus Michaelis [mailto:mickael+php@lupusmic.org
pusmic.org>
> ]
> > Sent: Friday, September 04, 2009 7:46 AM
> > To: php-general@lists.php.net
> > Subject: Re: [PHP] Converting URL's to hyperlinks.
> >
> > Daevid Vincent a =E9crit :
> > > Maybe I misunderstood the OP,
> > OP ?
>
> Original Poster
>
> > > but wouldn't this (or something like it) be
>
> Note the "something like it". I didn't write his app, just provided a
> starting point.
>
> > > $url =3D preg_replace("/(\w+@\w+\.[a-zA-Z]{2,3})/i", "
> > > href=3D'mailto:$1'>$1", $url);
> > This violate the numerous RFC about mail addresses, and some other
> > stuffs.
>
> Blah blah blah.
> I've used this code for about 6 years now and have yet to find emails tha=
t
> it didn't work for. If someone has some funky (whacky) RFC extremity, the=
n
> so be it. That's their problem. Most people have NORMAL emails that follo=
w
> the above.
>
> But you are correct, I have revised it to be a little more forgiving of
> some
> allowed characters...
>
> preg_replace("/([\w\.\-_]+@[\w\.\-_]+\.\w{2,6})/i",
>
> > >
> > > $url =3D
> > preg_replace("/\s(http:\/\/)?(\w*\.?\w*\.[a-zA-Z]{2,3}.*?\s) /i", "
> > > href=3D'http://$2' target=3D'_blank'>$2", $url);
> >
> > Same as previously. What about .info, .museum and so on tld ?
>
> What about them? It's going to depend on how/where you use this. In my
> case,
> 2 and 3 letter domains are all I encounter. It's trivial to make it {2,6}
> if
> you really are going to encounter a .museum domain. DOUBTFUL, but sure,
> I'll
> concede the three extra letters. ;-)
>
> However, you point out a few edge cases and so I optimized mine and now u=
se
> this one:
> http://snipplr.com/view/2371/regex-regular-expression-to-mat ch-a-url/
>
> > > Oh, and your regex isn't
> > smart (you use the case insensitivity flag, but seek A-Z
> > characters...) :D
>
> Noted. Thanks for the optimization.
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Thanks for posting that! Will be really handy for me!
--000e0cd1fadaeda2f50472c6b3cf--
Re: Converting URL"s to hyperlinks.
am 08.09.2009 16:59:39 von LuKreme
On 4-Sep-2009, at 14:49, Daevid Vincent wrote:
> I've used this code for about 6 years now and have yet to find =20
> emails that
> it didn't work for. If someone has some funky (whacky) RFC extremity,
It's attitudes like this that make the web such a wonderful place. =20
When I encounter a website the rejects my email address for being =20
'invalid" I simply NEVER GO THERE AGAIN. And your preg-replace would =20
reject every email address I ever input into any web form.
As a note, this will also fail on domains like http://â=AAdf.ws/, =
but I =20
guess Daring Fireball is an 'edge case' funky whacky RFC extremityâ=A6=
--=20
and I lift my glass to the Awful Truth / which you can't reveal to
the Ears of Youth / except to say it isn't worth a dime
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Converting URL"s to hyperlinks.
am 09.09.2009 03:22:25 von Lupus Michaelis
Sorry for the lag.
Daevid Vincent a écrit :
>> OP ?
>
> Original Poster
Thanks :)
> Blah blah blah.
> I've used this code for about 6 years now and have yet to find emails that
> it didn't work for. If someone has some funky (whacky) RFC extremity, then
> so be it. That's their problem. Most people have NORMAL emails that follow
> the above.
I faced a professionnal issue because of a lazy programmer (a Delphi
component that was not aware about "Toto man"@example.com). What you
consider normal e'mail is a subset that is not interoperable. And I hate
that.
> But you are correct, I have revised it to be a little more forgiving of some
> allowed characters...
>
> preg_replace("/([\w\.\-_]+@[\w\.\-_]+\.\w{2,6})/i",
You can revised your regex to fit to the new kind of email. But it is
smarter to use the right tool (like filter_vars).
Yeah, I know, I feel like some spanish windmill hunter.
--
Mickaël Wolff aka Lupus Michaelis
http://lupusmic.org
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Converting URL"s to hyperlinks.
am 09.09.2009 06:11:04 von Paul M Foster
On Wed, Sep 09, 2009 at 03:22:25AM +0200, Lupus Michaelis wrote:
> Sorry for the lag.
>
> Daevid Vincent a =E9crit :
>>> OP ?
>>
>> Original Poster
>
> Thanks :)
>
>> Blah blah blah.
>> I've used this code for about 6 years now and have yet to find emails =
that
>> it didn't work for. If someone has some funky (whacky) RFC extremity, =
then
>> so be it. That's their problem. Most people have NORMAL emails that fo=
llow
>> the above.
> I faced a professionnal issue because of a lazy programmer (a Delphi
> component that was not aware about "Toto man"@example.com). What you
> consider normal e'mail is a subset that is not interoperable. And I hat=
e
> that.
>
>> But you are correct, I have revised it to be a little more forgiving
> of some
>> allowed characters...
>>
>> preg_replace("/([\w\.\-_]+@[\w\.\-_]+\.\w{2,6})/i",
> You can revised your regex to fit to the new kind of email. But it is
> smarter to use the right tool (like filter_vars).
>
> Yeah, I know, I feel like some spanish windmill hunter.
I'm not sure it's well known on this list, but one resource I use for
regexps is:
http://www.regexlib.com/
Paul
--=20
Paul M. Foster
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php